package com.LeeCode;

/**
 * 最长公共前缀
 */

public class Code014 {
    public static void main(String[] args) {
        String[] strs = {"flower", "flow", "flight", ""};
        System.out.println(new Code014().longestCommonPrefix(strs));
    }

    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0)
            return "";

        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < strs[0].length(); i++) {
            boolean flag = true;
            for (int j = 0; j < strs.length; j++) {
                if (strs[j].length() == 0 || strs[j].length() - 1 < i || strs[j].charAt(i) != strs[0].charAt(i)) {
                    flag = false;
                }
            }
            if (!flag) {
                break;
            }
            ans.append(strs[0].charAt(i));
        }
        return ans.toString();
    }
}
